<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="formData.id?'编辑':'新增'" :visible="showdia" @close="btnCancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form v-if="showdia" ref="adddep" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @blur="checkname">
          <el-option v-for="val in employees" :key="val.id" :label="val.username" :value="val.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { getDepartments, addpart, getDepartDetail, updateDepartments } from '@/api/departments'
import { getemployeessimple } from '@/api/employees'
export default {
  props: {
    showdia: {
      type: Boolean,
      default: false
    },
    // 是tree-tool传给index,index传给这
    treeNode: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkname = async(rule, val, callback) => {
      // 所有的部门数据
      const { depts } = await getDepartments()
      let isbool
      if (this.formData.id) {
        // el.id !== this.treeNode.pid 父级的id就是子级的pid
        isbool = depts.filter(el => el.pid === this.treeNode.id && el.id !== this.treeNode.pid).some(el => el.name === val)
      } else {
        isbool = depts.filter(el => el.pid === this.treeNode.id).some(el => el.name === val)
      }
      if (isbool) {
        callback(new Error('部门不能重复'))
      } else {
        callback()
      }
    }
    const checkcode = async(rule, val, callback) => {
      // 所有的部门数据
      const { depts } = await getDepartments()
      let isbool
      if (this.formData.id) {
        isbool = depts.some(el => el.code === val && el.id !== this.treeNode.id)
      } else {
        isbool = depts.some(el => el.code === val)
      }
      if (isbool) {
        callback(new Error('部门编码不能重复'))
      } else {
        callback()
      }
    }
    return {
      // 定义表单数据
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkname, trigger: 'blur' }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkcode, trigger: 'blur' }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      },
      // 部门负责人
      employees: []
    }
  },
  async created() {
    this.employees = await getemployeessimple()
  },
  methods: {
    checkname() {
      setTimeout(() => {
        this.$refs.adddep.validateField('manager')
      }, 100)
    },
    async btnOk() {
      //
      // console.log(this.formData)
      //
      await this.$refs.adddep.validate()
      if (this.formData.id) {
        // 编辑
        await updateDepartments(this.formData)
        //
        this.$message.success('修改成功')
      } else {
        // 添加部门数据
        await addpart({
          ...this.formData,
          pid: this.treeNode.id
        })
        //
        this.$message.success('添加成功')
      }

      // 子传父 语法糖 关闭弹窗
      this.$emit('update:showdia', false)
      // 更新数据，重新渲染
      this.$emit('addpart')
    },
    btnCancel() {
      // 清除数据
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      this.$refs.adddep.resetFields()
      this.$emit('update:showdia', false)
    },
    async getDepartDetail(id) {
      this.formData = await getDepartDetail(id)
    }
  }
}
</script>

<style>

</style>
